package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.DeleteRatingRequest;
import com.movie.dto.RatingRequest;
import com.movie.dto.UserRatingDTO;
import com.movie.service.UserRatingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/user/ratings")
@RequiredArgsConstructor
@Tag(name = "用户评分管理", description = "用户对电影评分的增删改查接口")
public class UserRatingController {
    
    private final UserRatingService userRatingService;
    
    @PostMapping
    @Operation(summary = "添加或更新评分", description = "用户对电影进行评分")
    public ApiResponse<UserRatingDTO> addOrUpdateRating(
            @Parameter(description = "评分信息") @RequestBody RatingRequest request) {
        try {
            UserRatingDTO userRating = userRatingService.addOrUpdateRating(
                request.getUserId(), request.getMovieId(), request.getRating(), request.getComment());
            return ApiResponse.success("评分成功", userRating);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @DeleteMapping
    @Operation(summary = "删除评分", description = "用户删除对电影的评分")
    public ApiResponse<Void> removeRating(
            @Parameter(description = "删除评分请求") @RequestBody DeleteRatingRequest request) {
        try {
            userRatingService.removeRating(request.getUserId(), request.getMovieId());
            return ApiResponse.success("评分删除成功", null);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping
    @Operation(summary = "获取用户评分列表", description = "获取用户的所有评分记录")
    public ApiResponse<List<UserRatingDTO>> getUserRatings(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "电影ID", required = false) @RequestParam(required = false) Long movieId) {
        try {
            if (movieId != null) {
                List<UserRatingDTO> ratings = userRatingService.getMovieRatings(movieId);
                return ApiResponse.success(ratings);
            }
            List<UserRatingDTO> ratings = userRatingService.getUserRatings(userId);
            return ApiResponse.success(ratings);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/movie/{movieId}")
    @Operation(summary = "获取电影评分列表", description = "获取某部电影的所有用户评分")
    public ApiResponse<List<UserRatingDTO>> getMovieRatings(
            @Parameter(description = "电影ID") @PathVariable Long movieId) {
        try {


            List<UserRatingDTO> ratings = userRatingService.getMovieRatings(movieId);
            return ApiResponse.success(ratings);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/check")
    @Operation(summary = "检查是否评过分", description = "检查用户是否对某部电影评过分")
    public ApiResponse<Boolean> hasRated(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "电影ID") @RequestParam Long movieId) {
        try {
            boolean hasRated = userRatingService.hasRated(userId, movieId);
            return ApiResponse.success(hasRated);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/user-movie")
    @Operation(summary = "获取用户对电影的评分", description = "获取用户对某部电影的具体评分")
    public ApiResponse<UserRatingDTO> getUserRatingForMovie(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "电影ID") @RequestParam Long movieId) {
        try {
            Optional<UserRatingDTO> rating = userRatingService.getUserRatingForMovie(userId, movieId);
            return rating.map(ApiResponse::success)
                    .orElse(ApiResponse.error("未找到评分记录"));
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/movie/{movieId}/average")
    @Operation(summary = "计算电影平均评分", description = "计算某部电影的平均评分")
    public ApiResponse<Double> calculateMovieAverageRating(
            @Parameter(description = "电影ID") @PathVariable Long movieId) {
        try {
            Double averageRating = userRatingService.calculateMovieAverageRating(movieId);
            return ApiResponse.success(averageRating != null ? averageRating : 0.0);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/movie/{movieId}/count")
    @Operation(summary = "统计电影评分数量", description = "统计某部电影的评分数量")
    public ApiResponse<Long> countMovieRatings(
            @Parameter(description = "电影ID") @PathVariable Long movieId) {
        try {
            long count = userRatingService.countMovieRatings(movieId);
            return ApiResponse.success(count);
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
}